﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

using Shove.Database;


public partial class page_Options : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        isAtFramePageLogin = true;                              //是框架页
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url        
        RequestCompetences = Competences.BuildCompetencesList(Competences.Options);//页面权限

        base.OnLoad(e);
    }

    #endregion

    private void BindData()
    {
        DataSet ds = null;
        string ReturnDescription = "";

        int Result = DAL.Procedures.P_GetOptions(ref ds, ref ReturnDescription);

        if (ds == null || ds.Tables.Count < 2)
        {
            PublicFunction.GoError(ErrorNumber.NoData, "数据库访问出错", this.GetType().FullName);

            return;
        }

        foreach (DataRow group in ds.Tables[0].Rows)
        {
            TableCell tcGroup = new TableCell();

            tcGroup.ColumnSpan = 3;
            tcGroup.Text = group["Group"].ToString();
            tcGroup.Style.Value = "background-color:#E3EEFB; font-weight:bold; height:25px;";

            TableRow trGroup = new TableRow();

            trGroup.Controls.Add(tcGroup);
            table1.Controls.Add(trGroup);

            TableRow thr = new TableRow();
            TableCell[] thc = new TableCell[3];
            thc[0] = new TableCell();
            thc[1] = new TableCell();
            thc[2] = new TableCell();

            thc[0].Text = "选项";
            thc[1].Text = "值";
            thc[2].Text = "描述";
            thc[0].BackColor = Color.White;
            thc[1].BackColor = Color.White;
            thc[2].BackColor = Color.White;
            thc[0].Height = 25;

            thr.Cells.AddRange(thc);
            table1.Controls.Add(thr);

            foreach (DataRow dr in ds.Tables[1].Select("Group='" + group["Group"].ToString() + "'", "ID"))
            {
                TableRow tr = new TableRow();
                TableCell[] tc = new TableCell[3];
                tc[0] = new TableCell();
                tc[1] = new TableCell();
                tc[2] = new TableCell();

                tc[0].CssClass = "tcright";
                tc[1].CssClass = "tcleft";
                tc[2].CssClass = "tcleft";

                tc[0].Text = dr["Name"].ToString();

                bool isHasFTB = false;

                if (dr["Value"].ToString().ToLower() == "true" || dr["Value"].ToString().ToLower() == "false")
                {
                    //Bool型选项
                    RadioButtonList rbl = new RadioButtonList();
                    ListItem yes = new ListItem("是", "True");
                    ListItem no = new ListItem("否", "False");

                    rbl.ID = dr["key"].ToString();
                    rbl.Items.Add(yes);
                    rbl.Items.Add(no);
                    rbl.RepeatLayout = RepeatLayout.Flow;
                    rbl.RepeatDirection = RepeatDirection.Horizontal;

                    rbl.SelectedValue = dr["Value"].ToString().ToLower() == "true" ? "True" : "False";

                    tc[1].Controls.Add(rbl);
                }
                else
                {
                    if (dr["Value"].ToString().ToLower() == "bin")
                    {
                        //Bin型选项
                        isHasFTB = true;
                        tc[1].Text = "<a style='cursor:hand; color:Blue;' onclick=\"ShowFTB('tr_" + dr["key"].ToString() + "')\">点击编辑内容</a>";
                    }
                    else
                    {
                        TextBox tb = new TextBox();

                        tb.Width = 300;
                        tb.ID = dr["key"].ToString();
                        tb.Text = dr["Value"].ToString();
                        tc[1].Controls.Add(tb);
                    }
                }

                Label lb = new Label();

                lb.Text = dr["Description"].ToString();
                lb.Style.Value = "color:#A31515;";

                tc[2].Controls.Add(lb);

                tr.Cells.AddRange(tc);
                table1.Controls.Add(tr);

                if (isHasFTB)
                {
                    TableRow trFTB = new TableRow();
                    TableCell tcFTB = new TableCell();
                 //   FreeTextBox FTB = new FreeTextBox();

                    //FTB.ID = dr["key"].ToString();

                    //try
                    //{
                    //    FTB.Text = Shove._String.Decompress((byte[])dr["ValueBinary"]);
                    //}
                    //catch
                    //{
                    //    FTB.Text = "";
                    //}

                    //FTB.Width = 750;
                    //FTB.Height = 200;

                    tcFTB.BackColor = Color.White;
                    tcFTB.ColumnSpan = 3;
                    tcFTB.Style.Value = "padding-top:1px; padding-bottom:1px;";
                //    tcFTB.Controls.Add(FTB);

                    trFTB.ID = "tr_" + dr["key"].ToString();
                    trFTB.Cells.Add(tcFTB);
                    trFTB.Style.Value = "display:none;";

                    table1.Controls.Add(trFTB);
                }
            }
        }
    }

    protected void btnOK_Click(object sender, EventArgs e)
    {
        string[] key = Request.Form.AllKeys;

        if (key.Length < 1)
        {
            PublicFunction.GoError(ErrorNumber.Unknow, "致命错误，页面提交时没有获得参数组。", this.GetType().FullName);

            return;
        }

        for (int i = 1; i < key.Length - 2; i++)
        {
            SystemOptions so = new SystemOptions();

            so[key[i]] = new OptionValue(Request[key[i]]);
        }

        BindData();

        Shove._Web.JavaScript.Alert(this.Page, "保存成功");
    }
}